#!usr/bin/perl -w
=head1 Version V1.
Author: Zeng Liang,zlnineday@gmail.com
Date: 2016-04-04
=head1 Usage
perl rna_compare_analy.pl pro.fa.list [options]
-list                  <str>       input all sample nucleic fa (fa format)
-method                <str>       the method for construct evolation tree,nj ml bayes
-qsub                  <qsub>      whether qsub or not;default yes 
-help                              output help information to screen
=head1 Exmple
perl rna_compare_analy.pl pro.fa.list
pro.fa.list
name1   name1.pro.fa
name2   name2.pro.fa
...
=cut
use strict;
use Getopt::Long;
use FindBin qw($Bin $Script);
my ($list, $help, $qsub, $method);my ($shell, $result, $ref_index);	my $pwd=`pwd`;	chomp $pwd;
GetOptions(
    "list:s"=>\$list,
    "method:s"=>\$method,
    "qsub:s"=>\$qsub,
    "help"=>\$help
);
$qsub   ||="no";$method   ||="nj";

die `pod2text $0` if (@ARGV < 1 || $help);
open A,$ARGV[0];my @samp;
while(<A>){
    chomp;my @a=split;my @b=split /\//,$a[-1];
    my $id=$a[0].".pep.fa";
    `ln -s $a[1]`;
    `mv $b[-1] $a[0].pep.fa`;
    push @samp,$id;

}
close A;

my $sam=join ",",@samp;
if($qsub eq "yes"){
    print "#PBS -l nodes=1:ppn=8\n#PBS -q public01\ncd \$PBS_O_WORKDIR\n";
}
print "$sam\n";
`perl $Bin/orthomcl.pl --mode 1 --fa_files $sam >ortho.log.txt 2>ortho.log.txt`;
open IN,"ortho.log.txt";
while(<IN>){
    chomp;my @a=split;
    if(/Final ORTHOMCL Result: /){
        `cp $a[3] ./`;
    }
}
close IN;
`perl $Bin/select_ortholog_gene_pro.pl $ARGV[0] all_orthomcl.out`;
`perl $Bin/select_ortholog_gene_pro_add_nuc.pl $ARGV[0] muscle.out.list $list`;
if($method == "bayes"){
    print "perl $Bin/phylo_tree.pl input.phy -type $method -data dna";
}
else{
    print "perl $Bin/phylo_tree.pl input.phy -type $method -d nt";
}
open IN1,"orth.gene.nuc.clw.list";	
open IN2,">kaks.list";
while(<IN1>){
	chomp;my @a=split;
	`perl $Bin/produce_pop_kaks_sh.pl $a[1] $pwd/tree.nhx`;
	`codeml	`;
	print IN2 "$a[0]\t$a[0]..mlc.dN.dS.txt\n";
}
close IN1;	close IN2;
`perl $Bin/kaks_pvalue.pl kaks.list > Orth.gene.dN.dS.txt`;
